package com.xx.ems.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.xx.ems.model.Users;
import com.xx.ems.system.BaseController;

/**
 * @author xxx
 * @date 2014-2-25
 * @version 1.0
 * 描述: 用户CRUD操作
 */
@Controller
@RequestMapping(value="usersCRUD", method=RequestMethod.POST)
public class UsersCRUDController extends BaseController {
	/**
	 * 描述: 添加用户 
	 * @param users
	 * @return
	 * @date 2014-2-25
	 */
	@RequestMapping(value="/addUser")
	public void addUser(Users users, HttpServletResponse response) {
		try{
			boolean result = getUserCRUDService().addUser(users);;
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			if (result) {
				json.put("result", "suc");
			}else {
				json.put("result", "fail");
			}
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 修改用户 
	 * @param users
	 * @return
	 * @date 2014-2-25
	 */
	@RequestMapping(value="updateUser")
	public void updateUser(Users users, HttpServletResponse response) {
		try{
			boolean result = getUserCRUDService().updateUser(users);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			if (result) {
				json.put("result", "suc");
			}else {
				json.put("result", "fail");
			}
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 删除用户 
	 * @param id
	 * @return
	 * @date 2014-2-25
	 */
	@RequestMapping(value="deleteUser")
	public void deleteUser(int id, HttpServletResponse response) {
		try{
			boolean result = getUserCRUDService().deleteUser(id);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			if (result) {
				json.put("result", "suc");
			}else {
				json.put("result", "fail");
			}
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 判断输入用户账号是否存在,在则返回用户名,否则返回用户不存在 
	 * @param account
	 * @date 2014-3-2
	 */
	@RequestMapping(value="/isExistUser")
	public void isExistUser(String account, HttpServletResponse response) {
		try{
			String username = getUserCRUDService().isExistUser(account);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			if (username != null) {
				json.put("message", "用户名："+username);
			}else {
				json.put("message", "该用户不存在");
			}
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 根据用户的账号来获取用户信息 
	 * @param account
	 * @return
	 * @date 2014-2-28
	 */
	public Users getSpecificUser(String account) {
		Users user = getUserCRUDService().getSpecificUser(account);
		return user;
	}
	
	/**
	 * 描述: 获取用户列表 
	 * @param pageNow
	 * @param maxCount
	 * @param response
	 * @date 2014-3-4
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/getUsersList")
	public void getUsersList(@RequestParam(defaultValue="1") int pageNow, @RequestParam(defaultValue="8") int maxCount, HttpServletResponse response) {
		try{
			Map<String, Object> map = getUserCRUDService().getUsersList(pageNow, maxCount);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			List<Object> list = (List<Object>) map.get("list");
			if (list.size() > 0) {
				json.put("result", list);
			}else {
				json.put("result", "empty");
			}
			json.put("pageNow", pageNow);
			json.put("totalpage", map.get("totalpage"));
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}